from PySide6.QtWidgets import QDialog,QMessageBox
from views.schoolInfo.operationschoolinfo import Ui_operationschoolinfo
from sqlalchemy.orm import Session
from settings.publicvar import engine
from views.schoolInfo.model.SchoolInfo import SchoolInfo

class OperationSchoolInfo(QDialog,Ui_operationschoolinfo):
    def __init__(self,is_status,data):
        super().__init__()
        self.setupUi(self)
        self.if_status(is_status)
        self.setFormStructure(data)
        self.savebtn.clicked.connect(self.saveFun)
        self.changebtn.clicked.connect(self.changeFun)
        self.deletebtn.clicked.connect(self.deleteFun)
        self.restbtn.clicked.connect(self.resetFun)

    # 状态判断
    def if_status(self,is_status):
        if is_status == 'add':
            self.titlelable.setText('添加学校信息')
            self.deletebtn.hide()
            self.changebtn.hide()
        elif is_status == 'change':
            self.titlelable.setText('修改学校信息')
            self.savebtn.hide()
            self.deletebtn.hide()
        elif is_status == 'delete':
            self.titlelable.setText('删除学校信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.restbtn.hide()
        else:
            self.titlelable.setText('查看学校信息')
            self.savebtn.hide()
            self.changebtn.hide()
            self.restbtn.hide()
            self.deletebtn.hide()

    # 表单加载
    def setFormStructure(self,data):
        if data:
            self.idx.setText(data[0])
            self.name.setText(data[1])
            self.schoolnum.setText(data[2])
            self.introduction.setText(data[3])
            self.address.setText(data[4])
            self.mark.setText(data[5])

    # 获取表单数据
    def getFormData(self):
        idx = self.idx.text().strip()
        name = self.name.text().strip()
        schoolnum = self.schoolnum.text().strip()
        introduction = self.introduction.text().strip()
        address = self.address.text().strip()
        mark = self.mark.text().strip()

        data = {'name':name,'schoolnum':schoolnum,'introduction':introduction,'address':address,'mark':mark}
        if idx is None:
            return [False,data]
        else:
            return [idx,data]
    # 添加
    def saveFun(self):
        data = self.getFormData()
        with Session(engine) as session,session.begin():
            schoolinfo = SchoolInfo(**data[1])
            session.add(schoolinfo)
            QMessageBox.information(None,'成功','你添加%s成功！'%self.name.text())
            self.close()

    # 修改
    def changeFun(self):
        data = self.getFormData()
        with Session(engine) as session,session.begin():
            session.query(SchoolInfo).filter(SchoolInfo.id == data[0]).update(data[1])
            QMessageBox.information(None,'成功','你修改%s成功！'%self.name.text())
            self.close()

    # 删除
    def deleteFun(self):
        data = self.getFormData()
        with Session(engine) as session, session.begin():
            session.query(SchoolInfo).filter(SchoolInfo.id == data[0]).delete()
            QMessageBox.information(None, '成功', '你删除%s成功！' % self.name.text())
            self.close()

    # 重置
    def resetFun(self):
        self.name.setText('')
        self.schoolnum.setText('')
        self.introduction.setText('')
        self.address.setText('')
        self.mark.setText('')